package cn.kgc.novel.controller;

import cn.kgc.novel.common.Result;
import cn.kgc.novel.pojo.Type;
import cn.kgc.novel.pojo.Users;
import cn.kgc.novel.service.TypeService;
import cn.kgc.novel.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class TypeController {

    @Resource
    private TypeService typeService;
    @Resource
    private UsersService usersService;

    @GetMapping("/findType")
    public Result findType(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<Type> type = typeService.findType(null);
        PageInfo<Type> pageInfo=new PageInfo<Type>(type);
        return Result.ok(pageInfo);
    }
    //添加类型
    @PostMapping(value = "/addType")
    public Result addType(@RequestBody Type form){
        System.out.println(form.getSex()+"\t"+form.getCategory());
        typeService.addType(form);
        return Result.ok();
    }
    //删除类型
    @PostMapping(value = "deleteType")
    public Result deleteType(Integer tid){
        typeService.deleteType(tid);
        return Result.ok();
    }
    //统计人气
    @GetMapping(value = "/findPopularity")
    public Result findPopularity(String sex){
        List<Type> typeList = typeService.findType(sex);
        double sum=0;
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (Type type:typeList) {
            Map<String,Object> map=new HashMap<>();
            sum=0;
            List<Users> users = usersService.selectx1(type.getSex(), type.getCategory(), null, null);
            for (Users user:users) {
                double poplarity=Double.parseDouble(user.getBooks().getPopularity());
                System.out.println(user.getBooks().getPopularity());
                sum=sum+poplarity;
            }
            if (sum!=0){
                map.put("value",sum);
                map.put("name",type.getCategory()+"("+type.getSex()+")");
                mapList.add(map);
            }
        }
        return Result.ok(mapList);
    }
    //统计书籍数量
    @GetMapping(value = "/findBooksNum")
    public Result findBooksNum(String sex){
        List<Type> typeList = typeService.findType(sex);
        int num=0;
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (Type type:typeList) {
            Map<String,Object> map=new HashMap<>();
            num=0;
            List<Users> users = usersService.selectx1(type.getSex(), type.getCategory(), null, null);
            num=users.size();
            if(num!=0){
                map.put("value",num);
                map.put("name",type.getCategory()+"("+type.getSex()+")");
                mapList.add(map);
            }

        }
        return Result.ok(mapList);
    }
}
